package com.liuzy.movie;

import com.liuzy.util.UrlQueue;
import com.liuzy.util.Util;

/**
 * @author liuzy
 * @since 2016年3月2日
 */
public abstract class Movie {
	public UrlQueue pageQueue = new UrlQueue();
	public volatile boolean isRun = true;
	public int threadCount = 1;
	public DB _DB;
	public FILE _FILE;
	public boolean saveDB = false;
	public boolean saveFile = true;
	public void start(String startPage) {
		if (saveDB)
			_DB = new DB("127.0.0.1", 3306, "test", "root", "111111");
		if (saveFile)
			_FILE = new FILE("D:/Movie.html");
		pageQueue.add(startPage);
		pageStart();
	}
	public void stop() {
		isRun = false;
		pageQueue.clear();
		_DB.close();
		_FILE.close();
	}
	public void pageStart() {
		for (int i = 0; i < threadCount; i++) {
			new Thread(new Runnable() {
				public void run() {
					while (isRun) {
						String url = pageQueue.get();
						try {
							if (url == null)
								Thread.sleep(500);
							else {
								String html = Util.read2html(url);
								if (html != null)
									scaning(html);
							}
						} catch (Exception e) {
							pageQueue.history.remove(url);
						}
					}
				}
			}).start();
		}
	}
	public abstract void scaning(String html);
	public void save(String title, byte[] image, String download) {
		if (saveDB) {
			_DB.insert(title, image, download);
		}
		if (saveFile) {
			_FILE.append(title, download);
		}
	}
}
